﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIM.model;

namespace LIM.controller
{
    public class DanhMucHopDongController : BaseController
    {
        public DanhMucHopDongController()
        { 
        }
        public tbl_DanhMuc_HeThong LoadDataByRowID(int RowID)
        {
            return LIMS_Entities.tbl_DanhMuc_HeThong.Where(p => p.RowID == RowID).FirstOrDefault();
        }
        public string GetKH(int? RowIDKH)
        {
            string result = "";
            if (RowIDKH != null)
            {
                tbl_CRM_KhachHang a = LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.RowID == RowIDKH).FirstOrDefault();
                if (a != null)
                {
                    result = a.TenGoi;
                }
            }
            return result;
        }
        public string GetLienHe(int? RowIDLienHe)
        {
            string result = "";
            if (RowIDLienHe != null)
            {
                tbl_CRM_LienHe a = LIMS_Entities.tbl_CRM_LienHe.Where(p => p.RowID == RowIDLienHe).FirstOrDefault();
                if (a != null)
                {
                    result = a.HoTen;
                }
            }
            return result;
        }
        public tbl_CRM_HopDong LoadHopDongByRowID(int RowID)
        {
            return LIMS_Entities.tbl_CRM_HopDong.Where(p => p.RowID == RowID).FirstOrDefault();
        }
        public List<tbl_DanhMuc_HeThong> LoadDataList(string MaDanhMuc)
        {
            return LIMS_Entities.tbl_DanhMuc_HeThong.Where(p => p.MaDanhMuc == MaDanhMuc).OrderBy(p => p.ThuTu).ToList();
        }
        public List<tbl_DanhMuc_HeThong> LoadDataChild(int ParentID)
        {
            return LIMS_Entities.tbl_DanhMuc_HeThong.Where(p => p.RowIDParent == ParentID).OrderBy(o => o.ThuTu).ToList();
        }
        public List<object> LoadHopDong(bool? TinhTrang,string LoaiTinhTrang, bool IsOlder, int nam)
        {
            List<tbl_CRM_HopDong> lstHopDong = new List<tbl_CRM_HopDong>();
            if (TinhTrang == null)
            {
                lstHopDong = LIMS_Entities.tbl_CRM_HopDong.ToList();
            }
            else if (TinhTrang.Value)
            {
                lstHopDong = LIMS_Entities.tbl_CRM_HopDong.Where(p => p.TinhTrang == LoaiTinhTrang).ToList();
            }
            else
            {
                if (IsOlder)
                {
                    lstHopDong = LIMS_Entities.tbl_CRM_HopDong.ToList().Where(p => p.TinhTrang == LoaiTinhTrang && p.NgayTao.HasValue && p.NgayTao.Value.Year < nam).ToList();
                }
                else
                {
                    lstHopDong = LIMS_Entities.tbl_CRM_HopDong.ToList().Where(p => p.TinhTrang == LoaiTinhTrang && p.NgayTao.HasValue && p.NgayTao.Value.Year == nam).ToList();
                }
            }
            return lstHopDong.Select(p => new 
                                    {
                                        HD_PNM = "HD",
                                        RowID=p.RowID,
                                        Ma = p.MaHopDong,
                                        KhachHang = GetKH(p.RowIDKhachHang),
                                        DiaChi = p.DiaChi,
                                        NguoiLienHe = GetLienHe(p.RowIDLienLac),
                                        GhiChu=p.GhiChu,
                                        TinhTrang = p.TinhTrang != null ? LIMS_Entities.tbl_DanhMuc_HeThong.Where(o => o.MaDanhMuc == p.TinhTrang).FirstOrDefault().TenGoi : "",
                                        NgayKy=p.NgayTao,
                                        NgayKetThuc = p.ThoiGianHieuLuc_Den,
                                        NhanVienTao = p.NguoiTao,
                                        NgayTao=p.NgayTao,
                                        FileDinhKem=""
                                    }).ToList<object>();
        }
        public List<object> LoadPhieuNhanMau(bool? TinhTrang, string LoaiTinhTrang, bool IsOlder, int nam)
        {
            List<tbl_CRM_PhieuNhanMau> lstPhieuNhanMau = new List<tbl_CRM_PhieuNhanMau>();
            if (TinhTrang == null)
            {
                lstPhieuNhanMau = LIMS_Entities.tbl_CRM_PhieuNhanMau.ToList();
            }
            else if (TinhTrang.Value)
            {
                lstPhieuNhanMau = LIMS_Entities.tbl_CRM_PhieuNhanMau.Where(p => p.TinhTrang == LoaiTinhTrang).ToList();
            }
            else
            {
                if (IsOlder)
                {
                    lstPhieuNhanMau = LIMS_Entities.tbl_CRM_PhieuNhanMau.ToList().Where(p => p.TinhTrang == LoaiTinhTrang && p.NgayTao.HasValue && p.NgayTao.Value.Year < nam).ToList();
                }
                else
                {
                    lstPhieuNhanMau = LIMS_Entities.tbl_CRM_PhieuNhanMau.ToList().Where(p => p.TinhTrang == LoaiTinhTrang && p.NgayTao.HasValue && p.NgayTao.Value.Year == nam).ToList();
                }
            }
            return lstPhieuNhanMau.Select(p => new
            {
                HD_PNM = "PNM",
                RowID = p.RowID,
                Ma = p.MaPhieuNhanMau,
                KhachHang = GetKH(p.RowIDKhachHang),
                DiaChi = p.DiaChi,
                NguoiLienHe = GetLienHe(p.RowIDLienLac),
                GhiChu = p.GhiChuDanhChoKeToan,
                TinhTrang = p.TinhTrang != null ? LIMS_Entities.tbl_DanhMuc_HeThong.Where(o => o.MaDanhMuc == p.TinhTrang).FirstOrDefault().TenGoi : "",
                NgayKy = p.NgayTao,
                NhanVienTao = p.NguoiTao,
                NgayTao = p.NgayTao,
                FileDinhKem = ""
            }).ToList<object>();
        }
    }
    
}
